#include "connection.hpp"
void cb(NS_Channel::Channel::Ptr &channel, const std::string consumer_tag, const NS_Message::MessageProperties *bp, const std::string &body)
{
    channel->basicAck(bp->id());
    std::cout << consumer_tag << "消费了消息： " << body << std::endl;
}

int main(int argc, char *argv[])
{

    if (argc != 2)
    {
        std::cout << "usage: ./consume_client queueq\n";
        return -1;
    }
    // 1. 实例化连接对象 
    NS_AsyncWorker::AsyncWorker::Ptr async_worker = std::make_shared<NS_AsyncWorker::AsyncWorker>();
    NS_Connection::Connection::Ptr conn = std::make_shared<NS_Connection::Connection>("127.0.0.1", 8080, async_worker);

    // 2.通过连接创建信道
    NS_Channel::Channel::Ptr channel = conn->openChannel();
    // // 3.通过信道提供的服务完成所需

    //  1. 声明⼀个交换机exchange1,交换机类型为⼴播模式
    google::protobuf::Map<std::string, std::string> tmp_map;
    channel->declareExchange("exchange1", NS_Message::ExchangeType::TOPIC, true, false, tmp_map);

    //  2.声明⼀个队列queue1
    channel->declareMsgQueue("queue1", true, true, false, tmp_map);

    //  3.声明⼀个队列queue2
    channel->declareMsgQueue("queue2", true, true, false, tmp_map);

    //  4.绑定queue1 - exchange1，且binding_key设置为queue1
    channel->Bind("exchange1", "queue1", "queue");

    //  5.绑定queue2 - exchange1，且binding_key设置为news.music.#
    channel->Bind("exchange1", "queue2", "news.music.#");

    auto functor = std::bind(cb, channel, std::placeholders::_1,
                             std::placeholders::_2, std::placeholders::_3);

    channel->queueSubscription(argv[1], "consumer1", false, functor);
    
    while (conn->connected())
        std::this_thread::sleep_for(std::chrono::seconds(3));

    // conn
    if(conn->connected())
        conn->closeChannel(channel);
    return 0;
}